/**
 *
 */
package com.lmx.repoDomain.repository;

import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.persistence.service.EmployeeService;
import com.lmx.persistence.service.SalaryPositionService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.SalaryPosition;

/**
 * 职位薪资jsonapi
 * 
 * @author djs 2018年4月3日
 */
@JsonApiResourceRepository(SalaryPosition.class)
@Component
public class SalaryPositionRepository extends BaseRepository<SalaryPosition> {
	@Autowired
	private SalaryPositionService salaryPositionService;
	@Autowired
	private EmployeeService employeeService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(SalaryPosition.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	@JsonApiSave
	public SalaryPosition save(SalaryPosition salaryPosition) {
		if (null == salaryPosition.getId()) {
			return salaryPositionService.save(salaryPosition);
		} else if (1 == salaryPosition.getDelStatus()) {
			// 判断指定职位是否有有效员工数据（如果有不进行删除操作）
			List list = employeeService.findEmployeeExistSalaryPosition(salaryPosition);
			if (list.size() > 0) {
				throw new BusinessException(null, CommonConstants.ERROR_CODE.ADD_ERROR.label(),
						null);
			} else {
				return salaryPositionService.update(salaryPosition);
			}

		} else {
			return salaryPositionService.update(salaryPosition);
		}

	}
}
